package com.zlb.app.common.strategy.context;

import com.zlb.app.common.strategy.UploadStrategy;
import com.zlb.app.common.enums.UploadModeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Map;

/**
 * @Desc 文件上传策略上下文
 * @Author zWX1367238
 * @Date 2024-08-16 10:47:32
 **/
@Service
public class UploadStrategyContext {

    @Value("${upload.mode}")
    private String uploadMode;

    @Autowired
    private Map<String, UploadStrategy> uploadStrategyMap;

    /**
     * 以文件形式上传文件
     * @param file 文件
     * @param folder 文件保存在哪个文件夹下
     * @return 文件路径
     */
    public String upload(MultipartFile file, String folder) {
        return uploadStrategyMap.get(UploadModeEnum.getStrategy(uploadMode)).uploadFile(file, folder);
    }

    /**
     * 以流的形式上传文件,需要指定文件名
     * @param fileName
     * @param inputStream
     * @param folder 文件保存在哪个文件夹下
     * @return 文件路径
     */
    public String upload(String fileName, InputStream inputStream, String folder) {
        return uploadStrategyMap.get(UploadModeEnum.getStrategy(uploadMode)).uploadFile(fileName, inputStream, folder);
    }

}
